﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.IO;
using System.Net;
using HtmlExtractor;


namespace Reader.Domain
{
   
    public partial class BookPage
    {

        public string CurrentUri
        {
            get;
            set;
        }
         
        public string BookContentUri
        { get; set; }

        public string Author
        {
            get;
            set;
        }

        
        public string Catalog
        { get; set; }


        public string BookName
        {
            get;
            set;
        }
         
        public UriName NewChapter
        { get; set; }
        public UriName VipNewChapter
        { get; set; }

        public string LastUpdateTime
        {
            get;
            set;
        }
        public string VipLastUpdateTime
        {
            get;
            set;
        }
        public string Introduce
        {
            get;
            set;
        }
         
        public string ImageUri
        {get;set;}
    }
    public partial class BookPage
    {       
        private static void SaveXmlFile(BookPage bookPage, string uri)
        {
            string imagePath = ReaderPath.GetSiteFileName(bookPage.ImageUri);
            SaveFile.SaveImage(bookPage.ImageUri, imagePath,false,null);
            XmlWriterSettings ws = new XmlWriterSettings();
            ws.Indent = true;
            DataContractSerializer serializer = new DataContractSerializer(bookPage.GetType());
            try
            {
                string fileLoc = ReaderPath.GetSiteFileName(uri);
                if (!File.Exists(fileLoc))
                {
                    using (XmlWriter writer = XmlWriter.Create(File.Create(fileLoc), ws))
                    {
                        serializer.WriteObject(writer, bookPage);
                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void SaveXmlFile(string uri)
        {
            BookPage obj;
            string loacFile=null;
            if(ReaderPath.TryGetSiteFileName(uri,out loacFile))
            {
                return;
            }
            else if (Cache.Current.TryGet(uri, out obj))
            {
                SaveXmlFile(obj, uri);
            }
            else
            {
                WebClient client = new WebClient();
                client.Encoding = new Gb2312Encoding();
                client.DownloadStringAsync(new Uri(uri));
                client.DownloadStringCompleted += (s, args) =>
                {
                    if (args.Error == null)
                    {
                        obj = SaveFile.GenerateObject<BookPage>(args.Result, uri);
                        Cache.Current.Add(uri, obj);
                        SaveXmlFile(obj, uri);
                    }
                };

            }
        }
    }
}
